package library.GameLoader
{
	import GameMain.Game;
	
	import com.greensock.events.LoaderEvent;
	
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.media.Sound;
	import flash.utils.ByteArray;
	import flash.utils.getTimer;
	import flash.utils.setTimeout;
	
	public class GameLoader
	{
		
		public static const LOAD_TYPE_SWF:int = LoaderMain.LOAD_TYPE_SWF;
		public static const LOAD_TYPE_TEXT:int = LoaderMain.LOAD_TYPE_TEXT;
		public static const LOAD_TYPE_MAGIC:int = LoaderMain.LOAD_TYPE_MAGIC;
		public static const LOAD_TYPE_AS3XLS:int = LoaderMain.LOAD_TYPE_AS3XLS;
		public static const LOAD_TYPE_XML:int = LoaderMain.LOAD_TYPE_XML;
		public static const LOAD_TYPE_SOUND:int = LoaderMain.LOAD_TYPE_SOUND;
		public static const LOAD_TYPE_PACKAGE:int = LoaderMain.LOAD_TYPE_PACKAGE;
		
		public static const LOAD_STATE_ENTER_GAME:int = 1;	//进游戏的加载
		public static const LOAD_STATE_MODULE_DATA:int = 2;	//其它的加载
		public static const LOAD_STATE_BATTLE:int = 3;		//进	入战斗的加载
		
		private var _processState:int = 0;			//进度条的类型
		
		private var _compuleteFun:Function = null;
		private var _processFun:Function = null;
		
		
		
		private var _delayList:Array = null;
		private var _isLoading:Boolean = false;		
		
		
		private var _loaderMain:LoaderMain = null;
		
		
		public function GameLoader()
		{
			this._delayList = new Array();
			_loaderMain = new LoaderMain();
			
		}
		
		public function loadURLResVersion(version:String,ip:String, onLoadComplete:Function):void
		{
			_loaderMain.loadURLResVersion(version,ip,onLoadComplete);
		}
		
		/**
		 * 加载资源列表 显示进度条 尽量用此接口加载
		 * @param pathList 资源列表
		 * @param onCompleteFun  加载完后回调函数
		 */
		public function loadSourceArray(pathList:Array,onCompleteFun:Function,state:int = 0):void
		{
			if(_isLoading)
			{
				_delayList.push([pathList,onCompleteFun,state]);
				return;
			}
			_isLoading = true;
			_processState = state;
			_compuleteFun = onCompleteFun;
			if(_processFun != null)
				_processFun(0,null,_processState);
			_loaderMain.loadSourceArray(pathList,onCompulete,onProgressArray);
		}
		
		private function loadNext():void
		{
			if(_delayList.length)
			{
				var arr:Array = _delayList.shift();
				loadSourceArray(arr[0],arr[1],arr[2]);
			}
		}
		
		private function onCompulete(ar:Array):void
		{
			_isLoading = false;
			if(_processFun != null)
				_processFun(100,null,_processState);
			if (_compuleteFun != null)
				_compuleteFun(ar);			
			loadNext();
		}
		
		private function onError(p_e:Event, path:String = "") : void
		{
			Game.EngineMgr.printLog(p_e.toString(),path);
		}
		
		
		/**
		 * 检查一个文件路径的类型
		 * @return 类型请参考 GameLoader; 
		 */
		public function getloadType(path:String):int
		{
			return _loaderMain.getloadType(path);
		}
		
		
		private function onProgressArray(progress:int = 100,path:String = null):void
		{
			if(this._processFun != null)
			{				
				progress = progress > 98 ? 100: progress;
				this._processFun(progress,path,_processState);
			}
		}
		
		private function onProgressOne(path:String,progress:int = 100):void
		{
			return;
			if(this._processFun != null)
			{				
				progress = progress > 98 ? 100: progress;
				this._processFun(progress,LOAD_STATE_MODULE_DATA);
			}
		}
		
		public function setXMLDataCookie(data:ByteArray):void
		{
			_loaderMain.setXMLDataCookie(data);
		}
		
		/**
		 * 加载单份配置,不显示进度条
		 * @param path 路径
		 * @param onCompleteFun 回调函数
		 * @param ErrorFun 错误回调
		 */
		
		public function loadOneSource(path:String,onCompleteFun:Function,ErrorFun:Function = null,state:int = 0):void
		{
			_loaderMain.loadOneSource(path,onCompleteFun,onProgressOne,ErrorFun);
		}
		
		/**
		 * 拦截加载进度的接口
		 * @param processFun 需要处理加载进度的接口 
		 */
		public function holdUpProcessFun(processFun:Function):void
		{
			this._processFun = processFun;
		}
	}
}